<template>
    <el-dialog v-model="dialogFormVisible" :title="title" @close="closeDialog">
        <el-form :model="addBaseDepartmentForm" ref="addBaseDepartmentRef" :rules="addBaseDepartmentrules">
            <el-form-item label="所在年级" :label-width="formLabelWidth" prop="gradeId"
                v-if="addBaseDepartmentForm.type == 2">
                <el-select v-model="addBaseDepartmentForm.gradeId" autocomplete="off" @change="filterGradeId" clearable
                    style="width: 192px;" placeholder="请选择所在年级">
                    <el-option v-for="gradeId in yearData" :key="gradeId" :value="gradeId"></el-option>
                </el-select>
                <el-checkbox style="margin-left: 20px;" v-model="addBaseDepartmentForm.radio" label="复习班" size="large"
                    @change="checkboxHandle" />
            </el-form-item>
            <el-form-item :label="name" :label-width="formLabelWidth" prop="name">
                <el-input v-model="addBaseDepartmentForm.name" autocomplete="off" ref="name"
                    :placeholder="placeholder"></el-input>
            </el-form-item>
            <el-form-item label="父部门" :label-width="formLabelWidth" prop="parentId"
                v-if="addBaseDepartmentForm.type == 1">
                <el-select v-model="addBaseDepartmentForm.parentId" autocomplete="off" style="width: 100%;"
                    placeholder="请选择父部门" clearable>
                    <el-option v-for="item in departmentList" :key="item.id" :value="item.id"
                        :label="item.name"></el-option>
                </el-select>
            </el-form-item>
            <!-- <el-form-item label="权重" :label-width="formLabelWidth">
                <el-input v-model="addBaseDepartmentForm.orderNum" autocomplete="off" ref="name" type="number"
                    placeholder="请填写权重"></el-input>
            </el-form-item> -->
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="cancel()">取消</el-button>
                <el-button type="primary" @click="save()">保存</el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script>
import { ref } from "vue";
import { httpService } from "@/utils/httpService";
import { ElMessage, ElForm } from "element-plus";
import { vxRule } from "@/utils/validator";

export default {
    name: "AddBaseDepartment",
    components: { ElForm, ElMessage },
    data() {
        return {
            name: "",
            placeholder: "",
            dialogFormVisible: ref(false),
            formLabelWidth: "120px",
            title: "",
            personnelType: "",
            yearData: [],
            departmentList: [],
            user: {},
            addBaseDepartmentForm: {
                leader4: "",
                leader3: "",
                leader2: "",
                leader1: "",
                parentId: "",
                name: "",
                orderNum: "",
                type: "",
                campus: "",
                radio: "",
                gradeName: "",
                gradeId: "",
            },
            // 校验规则
            addBaseDepartmentrules: {
                leader4: vxRule(true, null, "blur"),
                leader3: vxRule(true, null, "blur"),
                leader2: vxRule(true, null, "blur"),
                leader1: vxRule(true, null, "blur"),
                name: vxRule(true, null, "blur"),
                gradeId: vxRule(true, null, "blur"),
            },
        };
    },
    mounted() {
        this.computedDate()
        this.user = JSON.parse(sessionStorage.getItem("user"))
    },
    methods: {
        computedDate() {
            let date = new Date();

            let nowYear = parseInt(date.getFullYear());
            let nowMonth = parseInt(date.getMonth() + 1);
            let gradeIds = [];
            if (nowMonth > 6) {
                gradeIds.push(nowYear - 2);
                gradeIds.push(nowYear - 1);
                gradeIds.push(nowYear);
            } else {
                gradeIds.push(nowYear - 3);
                gradeIds.push(nowYear - 2);
                gradeIds.push(nowYear - 1);
            }
            this.yearData = gradeIds
        },
        filterGradeId(value) {
            if (value) {
                this.addBaseDepartmentForm.name = value + "级" + this.addBaseDepartmentForm.gradeName
            } else {
                this.addBaseDepartmentForm.gradeId = ""
                this.addBaseDepartmentForm.name = ""
                this.addBaseDepartmentForm.radio = false
            }
        },
        checkboxHandle(value) {
            value ? this.addBaseDepartmentForm.gradeName = "F部" : this.addBaseDepartmentForm.gradeName = ""
            if (value && !this.addBaseDepartmentForm.gradeId) {
                this.addBaseDepartmentForm.name = "级F部"
            } else if (!value && this.addBaseDepartmentForm.gradeId) {
                this.addBaseDepartmentForm.name = this.addBaseDepartmentForm.gradeId + "级"
            } else {
                this.addBaseDepartmentForm.name = this.addBaseDepartmentForm.gradeId + "级" + this.addBaseDepartmentForm.gradeName
            }
        },
        closeDialog() {
            this.clear();
        },
        cancel() {
            this.dialogFormVisible = false;
            this.clear();
        },
        getBaseDepartmentById(id) {
            httpService("/javaApi/baseDepartment/queryById", { id: id }, "get").then(
                (data) => {
                    this.addBaseDepartmentForm = data;
                }
            );
        },
        getBaseDepartmentList() {
            httpService(
                "/javaApi/baseDepartment/list/1/100",
                {
                    type: this.addBaseDepartmentForm.type,
                    campus: this.user.campus,
                },
                "get"
            ).then((data) => {
                this.departmentList = data.list;
            });
        },
        clear() {
            this.dialogFormVisible = false;
            //清空form
            this.addBaseDepartmentForm.leader4 = "";
            this.addBaseDepartmentForm.leader3 = "";
            this.addBaseDepartmentForm.leader2 = "";
            this.addBaseDepartmentForm.leader1 = "";
            this.addBaseDepartmentForm.id = "";
            this.addBaseDepartmentForm.parentId = "";
            this.addBaseDepartmentForm.name = "";
            this.addBaseDepartmentForm.orderNum = "";
            this.addBaseDepartmentForm.type = "";
            this.addBaseDepartmentForm.campus = "";
            this.addBaseDepartmentForm.gradeId = ""
            this.addBaseDepartmentForm.radio = false
        },
        save() {
            this.$refs.addBaseDepartmentRef.validate((valid) => {
                if (valid) {
                    let params = new FormData(); //创建一个form对象,必须是form对象否则后端接受不到数据
                    if (this.addBaseDepartmentForm.type != 2) {
                        params.append("gradeId", 99999);
                    } else {
                        params.append("gradeId", this.addBaseDepartmentForm.gradeId);
                    }
                    params.append("parentId", this.addBaseDepartmentForm.parentId);
                    params.append("name", this.addBaseDepartmentForm.name);
                    params.append("orderNum", 99999);
                    params.append("type", this.addBaseDepartmentForm.type);
                    if (this.addBaseDepartmentForm.id) {
                        params.append("id", this.addBaseDepartmentForm.id);
                        httpService("/javaApi/baseDepartment/edit", params, "post").then(
                            (data) => {
                                if (data.code == 200) {
                                    ElMessage.success(data.object);
                                    this.clear();
                                    //刷新父组件的数据
                                    this.$parent.getBaseDepartmentList();
                                } else {
                                    ElMessage.error(data.message);
                                }
                            }
                        );
                    } else {
                        httpService("/javaApi/baseDepartment/add", params, "post").then(
                            (data) => {
                                if (data.code == 200) {
                                    ElMessage.success(data.object);
                                    this.clear();
                                    //刷新父组件的数据
                                    this.$parent.getBaseDepartmentList();
                                } else {
                                    ElMessage.error(data.message);
                                }
                            }
                        );
                    }
                } else {
                    return false;
                }
            });
        },
    },
};
</script>
<style></style>